<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div v-if="isTitle" class="area-header">
        <span class="area-header-title">{{ $t('融资信息') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('融资轮次')" prop="financingRound">
          <el-input v-model="queryParams.financingRound" :placeholder="$t('融资轮次')" show-word-limit maxlength="20" />
        </el-form-item>
        <el-form-item :label="$t('融资方式')" prop="financingMode">
          <el-select v-model="queryParams.financingMode" filterable :placeholder="$t('融资方式')" clearable>
            <el-option
              v-for="dict in financingModeOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('融资日期')" prop="financingDate">
          <DatePicker v-model="dateRange" type="daterange" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['company:finance:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
      </el-row>
    </div>

    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('融资信息') }}</span>
      </div>
      <div class="right">
        <el-button
          v-hasPermi="['company:finance:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="companyFinanceList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资轮次')" align="center" prop="financingRound" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.financingRound }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资方式')" align="center" prop="financingMode" :formatter="financingModeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('融资日期')" align="center" prop="financingDate" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.financingDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资总金额') + $t('金额单位')" align="center" prop="financingTotalAmount" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.financingTotalAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资前估值') + $t('金额单位')" align="center" prop="financingBeforeValuation" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.financingBeforeValuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('融资后估值') + $t('金额单位')" align="center" prop="financingAfterValuation" show-overflow-tooltip width="150">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.financingAfterValuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('币种')" align="center" prop="currency" :formatter="currencyFormat" show-overflow-tooltip />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:finance:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['company:finance:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改融资信息对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <companyFinanceForm ref="companyFinanceForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyFinance, delCompanyFinance, exportCompanyFinance } from '@/api/company/finance/companyFinance'
import companyFinanceForm from './companyFinanceForm.vue'
export default {
    components: {
        companyFinanceForm
    },
  props: {
    isTitle: {
      type: Boolean,
      default: true
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 融资信息表格数据
          companyFinanceList: [],
          // 弹出层标题
          title: '融资信息',
          // 是否显示弹出层
          formOpen: false,
        // 融资方式字典
        financingModeOptions: [],
        // 币种字典
        currencyOptions: [],
        // 时间范围
        dateRange: [],
          // 查询参数
          queryParams: {
              companyId: this.$route.query.companyId,
              financingRound: undefined,
              financingMode: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {}
      }
  },
  created() {
      this.getList()
      this.getDicts('financing_mode').then(response => {
          this.financingModeOptions = response.data
      })
      this.getDicts('currency').then(response => {
        this.currencyOptions = response.data
      })
  },
  methods: {
    /** 查询融资信息列表 */
    getList() {
        this.loading = true
        listCompanyFinance(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.companyFinanceList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 融资方式字典翻译
    financingModeFormat(row) {
      return this.selectDictLabel(this.financingModeOptions, row.financingMode)
    },
    // 币种字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            financingRound: undefined,
            financingMode: undefined,
            financingDate: undefined,
            financingTotalAmount: undefined,
            financingBeforeValuation: undefined,
            financingAfterValuation: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.dateRange = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.companyFinanceForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyFinanceForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delCompanyFinance(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportCompanyFinance(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    }
  }
}
</script>
